package com.gec.hrm.servlet;

import com.gec.hrm.dao.impl.NoticeDao;
import com.gec.hrm.dao.impl.NoticeDaoImpl;
import com.gec.hrm.dao.impl.TypeDao;
import com.gec.hrm.dao.impl.TypeDaoImpl;
import com.gec.hrm.pojo.Notice;
import com.gec.hrm.pojo.R;
import com.gec.hrm.pojo.Type;
import com.gec.hrm.pojo.User;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = {"/baoxiaoList.action","/delNotice.action","/baoxiaoAdmin.action","/addbaoxiao.action"})
public class NoticeServlet extends HttpServlet {
    public NoticeDao noticeDao = new NoticeDaoImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = request.getRequestURI();
        System.out.println(url);
        String action = url.substring(url.lastIndexOf("/") + 1);
        System.out.println(action);
        if (action.equals("baoxiaoList.action")) {
            noticeList(request, response);
        }else if(action.equals("baoxiaoAdmin.action")){
            noticeList(request,response);
        }else if(action.equals("addbaoxiao.action")){
            add(request,response);
        }else if(action.equals("delNotice.action")){
            delete(request,response);
        }
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) {
        int id=Integer.parseInt(request.getParameter("id"));

        int i = noticeDao.delete(id);

        try {
            PrintWriter out = response.getWriter();
            if (i > 0) {
                out.print(1);
            } else {
                out.print(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void add(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("title");
        String remark = request.getParameter("remark");
        HttpSession session=request.getSession();
        User user=(User)session.getAttribute("user");
        Notice notice = new Notice(name, remark,user.getId());
        int i = noticeDao.add(notice);

        try {
            PrintWriter out = response.getWriter();
            if (i > 0) {
                out.print(1);
            } else {
                out.print(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void noticeList(HttpServletRequest request, HttpServletResponse response) {
        //处理乱码问题
        response.setCharacterEncoding("UTF-8");
        //通过响应的response获取打印输出流
        PrintWriter out = null;
        try {
            out = response.getWriter();
            // 取出用户输入的条件
            //调用查询方法
            int page = Integer.parseInt(request.getParameter("page"));
            int limit = Integer.parseInt(request.getParameter("limit"));
            List<Notice> notices = noticeDao.noticeList(null,page, limit);
//            int count = deptDao.count();
            //lauyi框架返回四个参数
            R r = new R();
            r.put("msg", "查询成功");
            r.put("data", notices);
            r.put("count", notices.size());
            r.put("code", 0);
            //将对象返回到页面上，响应数据--respons--打印输出流
            out.print(new Gson().toJson(r));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("dopost");
        doGet(request, response);
    }
}
